{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 配置django环境"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a8102a33",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%capture #关闭日志输出\n",
    "import os\n",
    "import sys\n",
    "os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'biomind.settings')\n",
    "os.environ[\"DJANGO_ALLOW_ASYNC_UNSAFE\"] = \"true\"\n",
    "from django.core.wsgi import get_wsgi_application\n",
    "application = get_wsgi_application()\n",
    "import pydicom\n",
    "from pathlib import Path\n",
    "from django.db.models import OuterRef, Subquery\n",
    "from predicts.models import Predicts,Segments, Tasks\n",
    "from django.db.models import Count, F, Value\n",
    "from settings.utils.service_config import ServiceConf\n",
    "from toolz import pluck\n",
    "import datetime\n",
    "from pacs_uploader.models import PushbackTasks\n",
    "from common.log import logger\n",
    "from django.forms import model_to_dict\n",
    "from biomind.settings import BASE_DIR\n",
    "from bioutils.files import FileTool\n",
    "import socket\n",
    "from pyrsistent import m\n",
    "import hashlib\n",
    "from PIL import Image\n",
    "import numpy as np\n",
    "import tempfile\n",
    "import time\n",
    "import cv2\n",
    "from deepdiff import DeepDiff\n",
    "import requests\n",
    "import matplotlib.pyplot as plt\n",
    "from bioutils.series import SeriesClass\n",
    "import json\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70894478",
   "metadata": {},
   "source": [
    "# Test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12d081a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "if any([1,2,3]):\n",
    "    print(12345)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45791801",
   "metadata": {},
   "outputs": [],
   "source": [
    "path = '/lfs/biomind/predicts/1.3.46.670589.33.1.63746959433648976400001.5442206232043336355/biomind/08ec2555-126d-11ed-a006-ab35dbcdf5c3/'\n",
    "fn = path+\"payload.json\"\n",
    "payload = FileTool.read_json(fn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1651ae5b",
   "metadata": {},
   "outputs": [],
   "source": [
    "start_time = time.time()\n",
    "FileTool.write_ujson(payload, path+'payload_v2.json')\n",
    "print(time.time()-start_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58c236f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "start_time = time.time()\n",
    "with open(path+'payload_v3.json', \"w\") as f:\n",
    "    json.dump(payload, f)\n",
    "with open(path+'payload_v2.json', \"w\") as f:\n",
    "    json.dump(payload, f, indent=4)\n",
    "print(time.time()-start_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "787d601c",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = (1,2,[3])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb020a64",
   "metadata": {},
   "source": [
    "## 影核对接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "129dc12f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import time\n",
    "import uuid\n",
    "import requests\n",
    "import hashlib\n",
    "timestamp = str(int(round((time.time()) * 1000)))\n",
    "appKey = \"bwmlqbrS6AsZAc_2XntNwNwZGAbXFmki\"\n",
    "appSecret=\"U991zWcfGQvMpR5QuxXZ50Fk4dTr_8v-\"\n",
    "pwd = appKey + appSecret + timestamp\n",
    "sah1 = hashlib.sha1()\n",
    "bytestring = bytes(pwd, encoding='utf-8')\n",
    "sah1.update(bytestring)\n",
    "signature = sah1.hexdigest()\n",
    "headers = {\n",
    "    'content-type': 'application/json',\n",
    "    \"appkey\": appKey,\n",
    "    \"timestamp\": timestamp,\n",
    "    \"sign\": signature\n",
    "}\n",
    "uid = uuid.uuid1().hex\n",
    "print(\"uid\", uid)\n",
    "data = {\n",
    "  \"requestId\": uid,\n",
    "  \"studyUID\": \"1.2.840.113619.186.216157103242140.20200120095310650.775\",\n",
    "  \"appId\": [\n",
    "    \"tumor_svd\", \"hemo\"\n",
    "  ],\n",
    "  \"callBack\": \"http://192.168.10.183:8000/euler/yinghe/callback\",\n",
    "  \"dcm_urls\": {\"1.3.12.2.1107.5.2.30.26961.2020012010342828026108519.0.0.0\":'http://192.168.10.183:10000/volumes/1.2.840.113619.186.216157103242140.20200120095310650.775/1.3.12.2.1107.5.2.30.26961.2020012010343430061608554_1000.dicompkg.tar'\n",
    "  ,\"1.3.12.2.1107.5.2.30.26961.2020012010232679366905387.0.0.0\":\"http://192.168.10.183:10000/volumes/1.2.840.113619.186.216157103242140.20200120095310650.775/1.3.12.2.1107.5.2.30.26961.2020012010243918293606266_20.dicompkg.tar\"}\n",
    "}\n",
    "url='http://192.168.10.183/apiv3/euler/yinghe'\n",
    "# res = requests.post(url, headers=headers, data=json.dumps(data), verify=False)\n",
    "# res.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d696c4bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "s = requests.post(\"http://www.baidud.com\")\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a85e1535",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "url = 'https://192.168.10.183/apiv3/euler/yinghe?requestId=a595020ef5b711ecb217e316a1632322'\n",
    "\n",
    "data = requests.get(url, headers=headers, verify=False)\n",
    "data.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd1b7af9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import uuid\n",
    "data = {\n",
    "  \"requestId\": uuid.uuid1().hex,\n",
    "  \"studyUID\": \"1.2.840.113619.186.216157103242140.20200201085611828.920\",\n",
    "  \"appId\": [\n",
    "    \"tumor_svd\"\n",
    "  ],\n",
    "  \"callBack\": \"http://192.168.10.183:8000/euler/yinghe/callback\",\n",
    "  \"dcm_urls\": {\"1.2.840.113619.2.80.3040918456.31278.1647335594.18.13.2\":'http://192.168.10.183:10000/volumes/1.2.840.113619.6.408.140481875602756411605503867010767536370/1.2.840.113619.2.80.3040918456.31278.1647335594.19_16.dicompkg.tar',\n",
    "  \"1.2.840.113619.2.408.14196467.4242360.16609.1647302977.905\":\"http://192.168.10.183:10000/volumes/1.2.840.113619.6.408.140481875602756411605503867010767536370/1.2.840.113619.2.408.14196467.4242360.15703.1647303096.240_32.dicompkg.tar\"}\n",
    "}\n",
    "url='http://192.168.10.183:8000/euler/yinghe'\n",
    "requests.post(url, headers=headers, data=json.dumps(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "af6342f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import hashlib\n",
    "sah1 = hashlib.sha1()\n",
    "bytestring = bytes(\"12345678\", encoding='utf-8')\n",
    "sah1.update(bytestring)\n",
    "signature = sah1.hexdigest()\n",
    "print(signature, len(signature))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c15a8b0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "sah1 = hashlib.md5()\n",
    "bytestring = bytes(\"12345678\", encoding='utf-8')\n",
    "sah1.update(bytestring)\n",
    "signature = sah1.hexdigest()\n",
    "print(signature, len(signature))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22c49325",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = Predicts.objects.get(id=10)\n",
    "a.userid='hongwei'\n",
    "a.save(update_fields=['userid'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8eefc7cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "a.userid\n",
    "a.predictor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15375292",
   "metadata": {},
   "outputs": [],
   "source": [
    "from predicts.report_manager.generator.perfusion_report_generator import PerfusionReportGenerator\n",
    "from pprint import pprint\n",
    "predict = Predicts.objects.get(id=1049)\n",
    "#1061 ttp延长TEST^9\n",
    "#1056 1022322482 双侧不输出\n",
    "predict.prediction['structured_report']['vis_report']\n",
    "# structure_report = PerfusionReportGenerator(predict).generate_report({})\n",
    "# pprint(structure_report)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7f3aba8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "path = '/home/biomind/workspace/Biomind-Server/deps/Biomind-TestData/euler_task/ctp_prediction.json'\n",
    "jo = FileTool.read_json(path)\n",
    "jo['mandatory_non_spatial'].update({\n",
    "    \"structured_report\":{\n",
    "        'vis_report':predict.prediction['structured_report']['vis_report']\n",
    "    }\n",
    "})\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "786df140",
   "metadata": {},
   "outputs": [],
   "source": [
    "FileTool.write_json(jo, path)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e058816",
   "metadata": {},
   "source": [
    "## matplotlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "298d3a17",
   "metadata": {},
   "outputs": [],
   "source": [
    "atlas = \"sfsdfsd\"\n",
    "atlas"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15a7bbea",
   "metadata": {},
   "source": [
    "左侧血供分区大脑前动脉，大脑中动脉异常灌注。\n",
    "存在梗死，低灌注：\n",
    "梗死核心体积1.12ml,位于大脑前动脉左侧；\n",
    "低灌注区体积80.28ml,分别位于大脑前动脉左侧，大脑中动脉左侧；\n",
    "缺血半暗带...;\n",
    "失配体积...。\n",
    "左侧大脑前动脉较对侧：rCBF稍增高,rCBV稍增高；\n",
    "大脑中动脉较对侧：rCBF稍增高,rCBV稍增高；\n",
    "大脑后动脉较对侧：TTP稍延迟；\n",
    "其余血供分区左右侧未见明显差异。\n",
    "其中梗死区域：\n",
    "大脑中动脉较对侧：MTT稍延迟；\n",
    "...\n",
    "...\n",
    "...\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3e036a21",
   "metadata": {},
   "outputs": [],
   "source": [
    "# encoding:utf-8\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from pylab import mpl\n",
    "\"\"\"\n",
    "二次样条实现\n",
    "\"\"\"\n",
    "x = [3, 4.5, 7, 9]\n",
    "y = [2.5, 1, 2.5, 0.5]\n",
    "\n",
    "def calculateEquationParameters(x):\n",
    "    #parameter为二维数组，用来存放参数，sizeOfInterval是用来存放区间的个数\n",
    "    parameter = []\n",
    "    sizeOfInterval=len(x)-1\n",
    "    i = 1\n",
    "    #首先输入方程两边相邻节点处函数值相等的方程为2n-2个方程\n",
    "    while i < len(x)-1:\n",
    "        data = init(sizeOfInterval*3)\n",
    "        data[(i-1)*3]=x[i]*x[i]\n",
    "        data[(i-1)*3+1]=x[i]\n",
    "        data[(i-1)*3+2]=1\n",
    "        data1 =init(sizeOfInterval*3)\n",
    "        data1[i * 3] = x[i] * x[i]\n",
    "        data1[i * 3 + 1] = x[i]\n",
    "        data1[i * 3 + 2] = 1\n",
    "        temp=data[1:]\n",
    "        parameter.append(temp)\n",
    "        temp=data1[1:]\n",
    "        parameter.append(temp)\n",
    "        i += 1\n",
    "    #输入端点处的函数值。为两个方程,加上前面的2n-2个方程，一共2n个方程\n",
    "    data = init(sizeOfInterval*3-1)\n",
    "    data[0] = x[0]\n",
    "    data[1] = 1\n",
    "    parameter.append(data)\n",
    "    data = init(sizeOfInterval *3)\n",
    "    data[(sizeOfInterval-1)*3+0] = x[-1] * x[-1]\n",
    "    data[(sizeOfInterval-1)*3+1] = x[-1]\n",
    "    data[(sizeOfInterval-1)*3+2] = 1\n",
    "    temp=data[1:]\n",
    "    parameter.append(temp)\n",
    "    #端点函数值相等为n-1个方程。加上前面的方程为3n-1个方程,最后一个方程为a1=0总共为3n个方程\n",
    "    i=1\n",
    "    while i < len(x) - 1:\n",
    "        data = init(sizeOfInterval * 3)\n",
    "        data[(i - 1) * 3] =2*x[i]\n",
    "        data[(i - 1) * 3 + 1] =1\n",
    "        data[i*3]=-2*x[i]\n",
    "        data[i*3+1]=-1\n",
    "        temp=data[1:]\n",
    "        parameter.append(temp)\n",
    "        i += 1\n",
    "    return parameter\n",
    "\n",
    "\"\"\"\n",
    "对一个size大小的元组初始化为0\n",
    "\"\"\"\n",
    "def init(size):\n",
    "    j = 0\n",
    "    data = []\n",
    "    while j < size:\n",
    "        data.append(0)\n",
    "        j += 1\n",
    "    return data\n",
    "\n",
    "\n",
    "\"\"\"\n",
    "功能：计算样条函数的系数。\n",
    "参数：parametes为方程的系数，y为要插值函数的因变量。\n",
    "返回值：二次插值函数的系数。\n",
    "\"\"\"\n",
    "\n",
    "def solutionOfEquation(parametes,y):\n",
    "    sizeOfInterval = len(x) - 1\n",
    "    result = init(sizeOfInterval*3-1)\n",
    "    i=1\n",
    "    while i<sizeOfInterval:\n",
    "        result[(i-1)*2]=y[i]\n",
    "        result[(i-1)*2+1]=y[i]\n",
    "        i+=1\n",
    "    result[(sizeOfInterval-1)*2]=y[0]\n",
    "    result[(sizeOfInterval-1)*2+1]=y[-1]\n",
    "    a = np.array(calculateEquationParameters(x))\n",
    "    b = np.array(result)\n",
    "    return np.linalg.solve(a,b)\n",
    "\n",
    "\"\"\"\n",
    "功能：根据所给参数，计算二次函数的函数值：\n",
    "参数:parameters为二次函数的系数，x为自变量\n",
    "返回值：为函数的因变量\n",
    "\"\"\"\n",
    "def calculate(paremeters,x):\n",
    "    result=[]\n",
    "    for data_x in x:\n",
    "        result.append(paremeters[0]*data_x*data_x+paremeters[1]*data_x+paremeters[2])\n",
    "    return  result\n",
    "\n",
    "\n",
    "\"\"\"\n",
    "功能：将函数绘制成图像\n",
    "参数：data_x,data_y为离散的点.new_data_x,new_data_y为由拉格朗日插值函数计算的值。x为函数的预测值。\n",
    "返回值：空\n",
    "\"\"\"\n",
    "def  Draw(data_x,data_y,new_data_x,new_data_y):\n",
    "        plt.plot(new_data_x, new_data_y, label=u\"拟合曲线\", color=\"black\")\n",
    "        plt.scatter(data_x,data_y, label=u\"离散数据\",color=\"red\")\n",
    "        mpl.rcParams['font.sans-serif'] = ['SimHei']\n",
    "        mpl.rcParams['axes.unicode_minus'] = False\n",
    "        plt.title(u\"二次样条函数\")\n",
    "        plt.legend(loc=\"upper left\")\n",
    "        plt.show()\n",
    "\n",
    "result=solutionOfEquation(calculateEquationParameters(x),y)\n",
    "new_data_x1=np.arange(3, 4.5, 0.1)\n",
    "new_data_y1=calculate([0,result[0],result[1]],new_data_x1)\n",
    "new_data_x2=np.arange(4.5, 7, 0.1)\n",
    "new_data_y2=calculate([result[2],result[3],result[4]],new_data_x2)\n",
    "new_data_x3=np.arange(7, 9.5, 0.1)\n",
    "new_data_y3=calculate([result[5],result[6],result[7]],new_data_x3)\n",
    "new_data_x=[]\n",
    "new_data_y=[]\n",
    "new_data_x.extend(new_data_x1)\n",
    "new_data_x.extend(new_data_x2)\n",
    "new_data_x.extend(new_data_x3)\n",
    "new_data_y.extend(new_data_y1)\n",
    "new_data_y.extend(new_data_y2)\n",
    "new_data_y.extend(new_data_y3)\n",
    "Draw(x,y,new_data_x,new_data_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e0c457d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# aif_list = savgol_filter(aif_list, 5, 3, mode= 'nearest')\n",
    "# vof_list = savgol_filter(vof_list, 5, 3, mode= 'nearest')\n",
    "aif_interp1d_func = make_interp_spline(time_list, aif_list) # interp1d(time_list, aif_list, \"quadratic\")\n",
    "vof_interp1d_func = make_interp_spline(time_list, vof_list) #interp1d(time_list, vof_list, \"quadratic\")\n",
    "interpolated_aif = []\n",
    "interpolated_vof = []\n",
    "interpolated_time = []\n",
    "min_v = np.hstack((aif_list, vof_list)).min()\n",
    "max_v = np.hstack((aif_list, vof_list)).max()\n",
    "\n",
    "for i in range(len(time_list)-1):\n",
    "    x = np.linspace(time_list[i], time_list[i+1], 5)\n",
    "    y_1 = aif_interp1d_func(x)\n",
    "    y_2 = vof_interp1d_func(x)\n",
    "    for _i, y in enumerate(zip(y_1, y_2)):\n",
    "        if min_v <=y[0]<=max_v and min_v <=y[1]<=max_v:\n",
    "            interpolated_time.append(x[_i])\n",
    "            interpolated_aif.append(y[0])\n",
    "            interpolated_vof.append(y[1])\n",
    "\n",
    "plt.figure(figsize=(16, 9))\n",
    "plt.plot(interpolated_time, interpolated_aif, 'r')\n",
    "plt.scatter(time_list, aif_list, c=\"red\")\n",
    "plt.scatter(time_list, vof_list, c=\"blue\")\n",
    "plt.plot(interpolated_time, interpolated_vof, 'b')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45adc218",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8045ee3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from bioutils.transformer import DataTransformer\n",
    "import SimpleITK as sitk\n",
    "dcm_1 = '/lfs/biomind/orthanc/original/1.2.840.113619.2.379.141734675540075.78700.1638388266608.5/1.2.840.113619.2.379.141734675540075.78700.1638388266649.36/1.2.840.113619.2.379.141734675540075.78700.1638388266649.34.dcm'\n",
    "dcm = '/lfs/biomind/orthanc/original/1.2.392.200036.9116.2.6.1.3268.2060127208.1623480904.341754/1.2.392.200036.9116.2.6.1.3268.2060127208.1623899712.798026/1.2.392.200036.9116.2.6.1.3268.2060127208.1623899714.745769.dcm'\n",
    "data_sitk = DataTransformer.Dcm2Np(dcm)\n",
    "data_sitk[data_sitk< -1024] = -1024\n",
    "data_sitk+=1024"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9155f6f",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(data_sitk[0].max())\n",
    "print(data_sitk[0].min())\n",
    "plt.imshow(data_sitk[0], cmap=plt.cm.gray)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23295e2c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "left = 35-75\n",
    "right = 35+75\n",
    "res = data_sitk[0]\n",
    "res = res\n",
    "print(res.max(), res.min())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69d62fca",
   "metadata": {},
   "outputs": [],
   "source": [
    "res[res<left] = left\n",
    "res[res>right] = right\n",
    "res = (res-left)/(right-left)*255\n",
    "print(res.max(),res.min())\n",
    "plt.imshow(res, cmap=plt.cm.gray)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fedb2ae7",
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "import numpy as np\n",
    "\n",
    "from base64 import b64decode\n",
    "\n",
    "res = np.array(Image.open('/home/biomind/workspace/jupyterlab-dev/test_data/gao.png'))\n",
    "left = 0\n",
    "right = 51\n",
    "# res = res-1024\n",
    "res[res<left] = left\n",
    "res[res>right] = right\n",
    "res = (res-left)/(right-left)*255\n",
    "plt.imshow(res, cmap=plt.cm.gray)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3fe12b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "a = \"original_sfsdfsd\"\n",
    "a.removeprefix(\"originsal_\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63e06324",
   "metadata": {},
   "outputs": [],
   "source": [
    "import SimpleITK as sitk\n",
    "nrrd_path = '/lfs/biomind/predicts/1.2.826.0.1.1000052824.100294699.2022021807584364.4/biomind/4c0afc2f-c6a6-11ec-adeb-e316a1632322/perfusion_registration.nrrd'\n",
    "res = DataTransformer.NiiToNpy(nrrd_path)[0][0]\n",
    "left = -285\n",
    "right = 315\n",
    "# res = res-1024\n",
    "res[res<left] = left\n",
    "res[res>right] = right\n",
    "print(res.min(),res.max())\n",
    "res = (res-left)/(right-left)*255\n",
    "plt.imshow(res, cmap=plt.cm.gray)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# scripts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## requests test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "res = FileTool.read_json('/home/biomind/response.json')\n",
    "requests.post('http://192.168.10.183:10000/euler/update_task',json=res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "png_path = '/lfs/biomind/predicts/1639073465.3329294.34483.26/biomind/1c7c16af-c15c-11ec-add6-e316a1632322/mask/perfusion_ttp-10.png'\n",
    "img = np.array(Image.open(png_path))\n",
    "img.shape\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with tempfile.TemporaryDirectory() as temp:\n",
    "    start_time = time.time()\n",
    "    file_path = Path(temp) / 'test.png'\n",
    "    Image.fromarray(img).save(file_path)\n",
    "    print(time.time()-start_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with tempfile.TemporaryDirectory() as temp:\n",
    "    start_time = time.time()\n",
    "    file_path = str(Path(temp) / 'test.png')\n",
    "    cv2.imwrite(file_path, img)\n",
    "    print(time.time()-start_time)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## regular expression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## sqls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "{values[0] for values in Segments.objects.filter(predict_id=50).values_list(\"series_uid\")}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Segments.objects.filter(predict_id=39).values(\"category\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "overview_path = '/home/biomind/developer/jupyter-dev/test_data/overview'\n",
    "ct_25 = os.path.join(overview_path,'ct_25')\n",
    "cta_30 = os.path.join(overview_path,'braincta_30')\n",
    "ctp_4 = os.path.join(overview_path,'ctp_4')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Predicts.objects.values(\"predictor\").aggregate(pre_count=Count(\"predictor\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Predicts.objects.values(\"predictor\").annotate(count_model=Count('predictor')).filter(count_model__gt=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Tasks.objects.filter(predictor=\"brainctp_predictor\").aggregate(Max(\"status\"),Min(\"status\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.4 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  },
  "vscode": {
   "interpreter": {
    "hash": "3d597f4c481aa0f25dceb95d2a0067e73c0966dcbd003d741d821a7208527ecf"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
